﻿using System;
using UnityEngine;
using Random = UnityEngine.Random;

namespace TG
{
    public class Wolf : EntityBase
    {
        public override MapItemType mapItemType => MapItemType.Wolf;

        public float attackArea = 6f;

        public float idleSpeed = 1f;

        public float rotSpeed = 10f;

        public float moveSpeed = 1.5f;

        public Vector3 randomEndPos;

        public WolfAIType AIType;

        public Material mat;

        public GameObject dangerArea;

        protected override void Awake()
        {
            base.Awake();
            moveSpeed = GameManager.Instance.Player.moveSpeed * 1.5f;
        }

        public void InitDangerArea(GameObject dangerArea)
        {
            this.dangerArea = dangerArea;
            this.dangerArea.SetActive(true);
            this.dangerArea.transform.position =
                new Vector3(mapCell.position.x, mapCell.position.y + 0.1f, mapCell.position.z);
            AIType = WolfAIType.None;
        }

        private void Update()
        {
            if (AIType == WolfAIType.Stop)return;
            
            float player2WolfDis = CalcDistance();
            if (player2WolfDis < attackArea)
                AIType = WolfAIType.Run;
            else if (AIType == WolfAIType.Run ||
                     AIType == WolfAIType.Attack)
                AIType = WolfAIType.None;

            switch (AIType)
            {
                case WolfAIType.Idle:
                    JoinIdle();
                    break;
                case WolfAIType.Walk:
                    MoveInEndPos();
                    break;
                case WolfAIType.Run:
                    JoinRun();
                    break;
                case WolfAIType.None:
                    JoinIdle();
                    break;
                case WolfAIType.Attack:
                    JoinAttack();
                    break;
            }
        }

        protected override void Trigger(EntityBase entity)
        {
            if (entity is LambPlayer lambPlayer)
            {
                base.Trigger(entity);
                if (lambPlayer.GetCleat())
                {
                    //播放被撞飞动画
                    //销毁
                    RemoveSelf();
                    //狼被撞死+100
                    GameManager.Instance.GetGameData().AddScore(100);
                }
                else
                {
                    GameManager.Instance.GameReset();
                }
            }
        }

        private float CalcDistance()
        {
            return Vector3.Distance(mapCell.position, GameManager.Instance.Player.transform.position);
        }

        private void JoinIdle()
        {
            if (AIType == WolfAIType.Walk)
                return;
            AIType = WolfAIType.Walk;
            //随机巡逻结束点
            var randomX = Random.Range(-attackArea, attackArea);
            var randomZ = Random.Range(-attackArea, attackArea);
            randomEndPos = mapCell.position + new Vector3(randomX, 0, randomZ);
        }

        private void JoinRun()
        {
            var target = GameManager.Instance.Player.transform;
            var dir = target.position - transform.position;
            //转身
            if (Vector3.Angle(transform.forward, dir.normalized) < 5f)
            {
                transform.LookAt(target);

                if (Vector3.Distance(target.position, transform.position) <= idleSpeed * Time.deltaTime)
                {
                    //抓到羊 播放攻击动作
                    AIType = WolfAIType.Attack;
                }
                else
                {
                    transform.position += transform.forward * (moveSpeed * Time.deltaTime);
                }
            }
            else
                RotToTarget(target.position);
        }

        private void JoinAttack()
        {
            //游戏结束
        }

        private void MoveInEndPos()
        {
            var dir = randomEndPos - transform.position;
            //转身
            if (Vector3.Angle(transform.forward, dir.normalized) < 5f)
            {
                transform.LookAt(randomEndPos);

                if (Vector3.Distance(randomEndPos, transform.position) <= idleSpeed * Time.deltaTime)
                {
                    transform.position = randomEndPos;
                    //到达巡逻终点 播下idle 然后再运动
                    AIType = WolfAIType.Idle;
                }
                else
                {
                    transform.position += transform.forward * (idleSpeed * Time.deltaTime);
                }
            }
            else
                RotToTarget(randomEndPos);
        }

        private void RotToTarget(Vector3 target)
        {
            var qua = Quaternion.LookRotation((target - transform.position).normalized, Vector3.up);
            transform.localRotation = Quaternion.Slerp(transform.localRotation, qua, rotSpeed * Time.deltaTime);
        }

        public override void RemoveSelf()
        {
            dangerArea.SetActive(false);
            base.RemoveSelf();
            AIType = WolfAIType.Stop;
        }
    }
}